package com.gimi.cloud.jimi.entity.model;

import com.gimi.cloud.jimi.common.utils.Page;
import com.gimi.cloud.jimi.entity.sys.SysConfigEntity;
import org.springframework.beans.BeanUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiankejin on 2017/10/16
 */
public class SystemConfigModel extends SysConfigEntity {

    /** 当前页码 */
    private int page;
    /** 每页总数 **/
    private int rows;

    /** 每页数据条数 */
    private int size = Page.ROWS;

    private Map<String, SysConfigEntity> map;

    /** 系统配置表nid数组 */
    private String[] configNid;

    /** 条件查询 */
    private String searchName;


    private int status;

    private int type;



    public Integer getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Integer getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }


    public SystemConfigModel() {
        map = Collections.synchronizedMap(new HashMap<String, SysConfigEntity>());
    }

    public SystemConfigModel(int size) {
        map = Collections.synchronizedMap(new HashMap<String, SysConfigEntity>());
        configNid = new String[size];
    }

    public static SystemConfigModel instance(SysConfigEntity sconfig) {
        SystemConfigModel systemConfigModel = new SystemConfigModel();
        BeanUtils.copyProperties(sconfig, systemConfigModel);
        return systemConfigModel;
    }

    public SysConfigEntity prototype() {
        SysConfigEntity sconfig = new SysConfigEntity();
        BeanUtils.copyProperties(this, sconfig);
        return sconfig;
    }

    public void addConfig(SysConfigEntity sys) {
        map.put(sys.getCfKey().replace("con_", ""), sys);
    }

    public void setConfigNid(int length, String nid){
        configNid[length] = nid.replace("con_", "");
    }

    private SysConfigEntity getConfig(String key) {
        SysConfigEntity sys = (SysConfigEntity) map.get(key);
        return sys;
    }

    public String getValue(String key) {
        SysConfigEntity c = getConfig(key);
        if (c == null)
            return null;
        return c.getCfValue();
    }

    public String getStatus(String key) {
        SysConfigEntity c = getConfig(key);
        if (c == null)
            return "normal";
        return c.getCfStatus();
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSize() {
        return size > 0 ? size : Page.ROWS;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public Map<String, SysConfigEntity> getMap() {
        return map;
    }

    public void setMap(Map<String, SysConfigEntity> map) {
        this.map = map;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getSearchName() {
        return searchName;
    }

    public void setSearchName(String searchName) {
        this.searchName = searchName;
    }

    public String[] getConfigNid() {
        return configNid;
    }

    public void setConfigNid(String[] configNid) {
        this.configNid = configNid;
    }

}
